package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     *
     * @return
     */
    @Override
    public void add(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);

        shoppingCart.setUserId(BaseContext.getCurrentId());
        //判断是否存在记录
        List<ShoppingCart> list = shoppingCartMapper.getBy(shoppingCart);
        if (list != null && list.size() >0){// 存在购物车商品份数加1
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1);
            shoppingCartMapper.update(cart);
        }else {//不存在，新增
            //判断是菜品还是套餐
            if (dto.getDishId() != null) {
                //根据菜品id查询菜品信息
                Dish dish = dishMapper.getById(dto.getDishId());
                //设置初始值
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                //根据套餐id查询套餐信息
                SetmealVO query = setmealMapper.query(dto.getSetmealId());
                //设置初始值
                shoppingCart.setName(query.getName());
                shoppingCart.setImage(query.getImage());
                shoppingCart.setAmount(query.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }

    }

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.getBy(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void delete() {
        ShoppingCart shoppingCart = ShoppingCart.builder().
                userId(BaseContext.getCurrentId())
                .build();
        shoppingCartMapper.delete(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     *
     * @param dto
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto,shoppingCart);
        //只获取当前一个用户的购物车
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //获取购物车商品信息
        List<ShoppingCart> carts = shoppingCartMapper.getBy(shoppingCart);
        ShoppingCart cart = carts.get(0);
        //判断购物车商品份数
        if (cart.getNumber() == 1){
            shoppingCartMapper.delete(cart);
        }else {
            cart.setNumber(cart.getNumber()-1);
            shoppingCartMapper.update(cart);
        }
    }
}
